package LeetDev.a3d;

import javax.microedition.khronos.opengles.GL10;

public class Light extends Node {
	public static final int Omni=1;
	public static final int Directional=2;
	public static final int Spot=3;
	
	private static int s_sceneLights;

	private Color m_color;
	private int m_type;
	private Vect m_direction;
	private float m_exponent;
	private float m_cutoff;
	private float m_attenuation;
	
	private static float p[]=new float[4];
	
	public Light(final Vect pos,final Color color, float attenuation,boolean directional)
	{
		super();
		setColor(color);
		setAttenuation(attenuation);
		setCutOff(180);
		if(directional)
		{
			setDirection(pos);
			setType(Directional);
		}
		else
		{
			setPosition(pos);
			setType(Omni);
		}
	}

	public Light(final Vect pos,final Color color,final Vect direction, float exponent, float cutoff, float attenuation)
	{
		super();
		setColor(color);
		setAttenuation(attenuation);
		setCutOff(cutoff);
		setDirection(direction);
		setExponent(exponent);
		setType(Spot);
		setPosition(pos);
	}

	public void draw(GL10 gl)
	{
		// we need to activate the light immediately.
		if(s_sceneLights<Engine.instance().GetMaxLights())
		{
			// we can use the light!
			gl.glMatrixMode(GL10.GL_MODELVIEW);
			gl.glLoadIdentity();
			int thelight=GL10.GL_LIGHT0+s_sceneLights;
			gl.glEnable(thelight);
			p[0]=m_color.r;
			p[1]=m_color.g;
			p[2]=m_color.b;
			p[3]=m_color.a;
			gl.glLightfv(thelight,GL10.GL_DIFFUSE,p,0);
			gl.glLightfv(thelight,GL10.GL_SPECULAR,p,0);
			if(m_attenuation>=0)
			{
				gl.glLightf(thelight,GL10.GL_CONSTANT_ATTENUATION,0);
				gl.glLightf(thelight,GL10.GL_LINEAR_ATTENUATION,m_attenuation);
			}
			else
			{
				gl.glLightf(thelight,GL10.GL_CONSTANT_ATTENUATION,1);
				gl.glLightf(thelight,GL10.GL_LINEAR_ATTENUATION,0);
			}
			switch(m_type)
			{
				case Omni:
				{
					p[0]=m_position.x;
					p[1]=m_position.y;
					p[2]=m_position.z;
					p[3]=1;
					gl.glLightfv(thelight,GL10.GL_POSITION,p,0);
					gl.glLightf(thelight,GL10.GL_SPOT_CUTOFF,180.0f);
					break;
				}
				case Directional:
				{
					p[0]=m_direction.x;
					p[1]=m_direction.y;
					p[2]=m_direction.z;
					p[3]=0;
					gl.glLightfv(thelight,GL10.GL_POSITION,p,0);
					gl.glLightf(thelight,GL10.GL_SPOT_CUTOFF,180.0f);
					break;
				}
				case Spot:
				{
					p[0]=m_position.x;
					p[1]=m_position.y;
					p[2]=m_position.z;
					p[3]=1;
					gl.glLightfv(thelight,GL10.GL_POSITION,p,0);
					p[0]=m_direction.x;
					p[1]=m_direction.y;
					p[2]=m_direction.z;
					gl.glLightfv(thelight,GL10.GL_SPOT_DIRECTION,p,0);
					gl.glLightf(thelight,GL10.GL_SPOT_EXPONENT,m_exponent);
					gl.glLightf(thelight,GL10.GL_SPOT_CUTOFF,m_cutoff);
					break;
				}
			}
			s_sceneLights++;
		}
	}
	
	public static void resetLights(GL10 gl)
	{
		s_sceneLights=0;
		int max=Engine.instance().GetMaxLights();
		for(int i=0;i<max;i++)
		{
			gl.glDisable(GL10.GL_LIGHT0+i);
		}
	}
	
	public Color getColor()
	{
		return(m_color);
	}
	public int getType()
	{
		return(m_type);
	}
	public Vect getDirection()
	{
		return(m_direction);
	}
	public float getExponent()
	{
		return(m_exponent);
	}
	public float getCutOff()
	{
		return(m_cutoff);
	}
	public float getAttenuation()
	{
		return(m_attenuation);
	}

	public void setColor(final Color color)
	{
		m_color=color;
	}
	public void setColor(final float r, final float g, final float b)
	{
		m_color.r=r;
		m_color.g=g;
		m_color.b=b;
	}
	public void setType(int type)
	{
		m_type=type;
	}
	public void setDirection(final Vect direction)
	{
		m_direction=direction;
	}
	public void setDirection(final float x, final float y, final float z)
	{
		m_direction.x=x;
		m_direction.y=y;
		m_direction.z=z;
	}
	public void setExponent(final float exponent)
	{
		m_exponent=exponent;
	}
	public void setCutOff(final float cutoff)
	{
		m_cutoff=cutoff;
	}
	public void setAttenuation(final float attenuation)
	{
		m_attenuation=attenuation;
	}

	
}
